package cn.tansanqinger.service.knowledge.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.catalogue.CatalogueSqlDaoImpl;
import cn.tansanqinger.pojo.Catalogue;
import cn.tansanqinger.service.knowledge.IKnowledgeMenuService;
import cn.tansanqinger.util.Tools;

@Service
public class KnowledgeMenuServiceImpl implements IKnowledgeMenuService{
	
	@Autowired
	private CatalogueSqlDaoImpl impl;

	@Override
	public Map<String, Object> selectMessage(HttpServletRequest request) {
		String keywords = Tools.isEmpty(request.getParameter("keywords"));
		String kispopup = Tools.isEmpty(request.getParameter("ispopup"));
		String page = Tools.isEmpty(request.getParameter("page"));
		String limit = Tools.isEmpty(request.getParameter("limit"));
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int kispopupInt = -1;
		if(!"".equals(kispopup)) {
			kispopupInt = Integer.parseInt(kispopup);
			if(kispopupInt<0||kispopupInt>1) {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据错误");
				return resultMap;
			}
		}
		int pageInt = 1;
		if(!"".equals(page)) {
			pageInt = Integer.parseInt(page);
		}
		int limitInt = 10;
		if(!"".equals(limit)) {
			limitInt = Integer.parseInt(limit);
		}
		return impl.selectMessage(keywords, kispopupInt, pageInt, limitInt);
	}

	@Override
	public Map<String, Object> serialNumber(int ctier) {
		Integer csn = impl.serialNumber(ctier);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code","0");
		map.put("msg", csn);
		return map;
	}

	@Override
	public Map<String, Object> add(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Catalogue catalogue = new Catalogue();
		String ctier = Tools.isEmpty(request.getParameter("ctier"));
		if("".equals(ctier)) {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
			return resultMap;
		}
		catalogue.setCno(impl.serialNumber(Integer.parseInt(ctier)));
		catalogue.setCname(Tools.isEmpty(request.getParameter("cname")));
		catalogue.setCpath("/");
		catalogue.setCtier(Integer.parseInt(ctier));
		catalogue.setIspopup(Integer.parseInt(Tools.isEmpty(request.getParameter("ispopup"))));
		String ctop = Tools.isEmpty(request.getParameter("ctop"));
		if(!"#".equals(ctop)) {
			catalogue.setCtop(Integer.parseInt(ctop));
		}
		catalogue.setCfinish(false);
		if("3".equals(ctier)) {
			catalogue.setCfinish(true);
		}
		if(impl.add(catalogue)==1) {
			resultMap.put("code","0");
			resultMap.put("msg", "添加成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "添加失败");
		}
		return resultMap;	
	}

	@Override
	public Map<String, Object> updateStatus(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id.matches("\\d+")) {
			Catalogue object = impl.selectOnt(Integer.parseInt(id));
			object.setIspopup(object.getIspopup()==0?1:0);
			if(impl.updateStatus(object)==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "修改成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "修改失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public Catalogue selectIdOne(String id) {
		return impl.selectOnt(Integer.parseInt(id));
	}

	@Override
	public Map<String, Object> delete(String id, String sn) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id.matches("\\d+")&&sn.matches("\\d+")) {
			if(impl.deleteOne(Integer.parseInt(id), Integer.parseInt(sn))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> modification(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Catalogue catalogue = selectIdOne(Tools.isEmpty(request.getParameter("cid")));
		catalogue.setCname(Tools.isEmpty(request.getParameter("cname")));
		if(impl.updateStatus(catalogue)==1) {
			resultMap.put("code","0");
			resultMap.put("msg", "修改成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "修改失败");
		}
		return resultMap;
	}

	@Override
	public List<Catalogue> selectMenu(int ctop) {
		return impl.selectMenu(ctop);
	}

}
